package com.yimils.core.sys.beanUtils;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yimils.core.sys.entity.RoleEntity;
import com.yimils.core.sys.entity.UserRoleEntity;
import com.yimils.core.sys.service.RoleService;
import com.yimils.core.sys.service.UserRoleService;
import com.yimils.core.sys.vo.UserRoleVo;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Component
public class UserRoleVoBeanUtils {

    @Resource(name = "userRoleService")
    private UserRoleService userRoleService;

    @Resource(name = "roleService")
    private RoleService roleService;

    public IPage<UserRoleVo> deepClone(IPage<UserRoleEntity> src) {
        IPage<UserRoleVo> pages = new Page<>();
        pages.setCurrent(src.getCurrent());
        pages.setSize(src.getSize());
        pages.setTotal(src.getTotal());
        pages.setPages(src.getPages());
        pages.setRecords(cloneRecords(src.getRecords()));
        return pages;
    }


    public List<UserRoleVo> cloneRecords(List<UserRoleEntity> src) {
        List<UserRoleVo> recordsVo = new ArrayList<>();
        for (UserRoleEntity entity : src) {
            UserRoleVo vo = new UserRoleVo();
            vo.setId(entity.getId());
            RoleEntity roleEntity = roleService.getRoleByUserId(entity.getUserId());
            vo.setName(roleEntity.getName());
            vo.setTitle(roleEntity.getTitle());
            vo.setType(entity.getType());
            recordsVo.add(vo);
        }
        return recordsVo;
    }

}
